package cc.vv.btpartyjob.partyservice.delegate

import android.content.Context
import android.support.v4.widget.SwipeRefreshLayout
import android.support.v7.widget.LinearLayoutManager
import android.support.v7.widget.RecyclerView
import android.text.TextUtils
import android.view.View
import android.widget.LinearLayout
import android.widget.TextView
import cc.vv.baselibrary.util.ColorUtil
import cc.vv.baselibrary.util.LKAppUtil
import cc.vv.baselibrary.view.BCFeedView
import cc.vv.baselibrary.view.LKEditText
import cc.vv.btpartyjob.partyservice.R
import cc.vv.btpartyjob.partyservice.adapter.PersonSearchAdapter
import cc.vv.btpartyjob.partyservice.module.bean.JobInfoBean
import cc.vv.mvp.delegate.BaseAppDelegate
import com.chad.library.adapter.base.BaseQuickAdapter

/**
 * 求职搜索托管类
 * Created by Chen.qi on 2018/10/24.
 */
class JobHunterSearchActivityDelegate : BaseAppDelegate() {

    override fun initWidget() {
    }

    override fun initLayoutRes(): Int {
        return R.layout.activity_station_search
    }

    /**
     * 设置历史数据
     */
    fun setHistoryData(historyList: ArrayList<String>?) {
        val historyView = getView<BCFeedView>(R.id.bfv_ass_history)
        historyView?.initStringData(historyList, true)
    }

    /**
     * 设置点击回调
     */
    fun bindTVOnClick(tvOnclickInterface: BCFeedView.TVOnClick) {
        val historyView = getView<BCFeedView>(R.id.bfv_ass_history)
        historyView?.tvOnClick = tvOnclickInterface
    }

    /**
     * 设置数据
     */
    fun setTextStr(str: String?) {
        val etSearch = getView<TextView>(R.id.et_ass_inputSearch)
        etSearch?.text = str
        setCursorLocation(str)
    }

    /**
     * 获取editText 数据
     */
    fun getTextStr(): String {
        val etSearch = getView<TextView>(R.id.et_ass_inputSearch)
        val textStr: String
        textStr = etSearch?.text.toString().trim()
        return textStr
    }

    /**
     * 设置光标位置
     */
    private fun setCursorLocation(str: String?) {
        if (!TextUtils.isEmpty(str)) {
            val etSearch = getView<LKEditText>(R.id.et_ass_inputSearch)
            etSearch?.setSelection(str!!.length)
        }
    }

    /**
     * 设置刷新监听
     */
    fun bindRefreshListener(listener: SwipeRefreshLayout.OnRefreshListener) {
        val refresh = getView<SwipeRefreshLayout>(R.id.srl_ass_swipeSearch)
        refresh?.setOnRefreshListener(listener)
        refresh?.setColorSchemeColors(ColorUtil.setBCColor(R.color.color_FA555C))
    }

    /**
     * 设置刷新停止
     */
    fun stopRefreshing() {
        val refresh = getView<SwipeRefreshLayout>(R.id.srl_ass_swipeSearch)
        refresh?.isRefreshing = false
    }

    /**
     * 设置加载更多监听
     */
    fun bindLoadMore(mAdapter: PersonSearchAdapter?, loadMoreListener: BaseQuickAdapter.RequestLoadMoreListener) {
        val recyclerView = getView<RecyclerView>(R.id.rv_ass_searchList)
        if (null != recyclerView) {
            mAdapter?.setOnLoadMoreListener(loadMoreListener, recyclerView)
        }
    }


    /** 设置适配器*/
    fun setRecyclerViewAdapter(mAdapter: PersonSearchAdapter?) {
        if (null != mAdapter) {
            val recyclerView = getView<RecyclerView>(R.id.rv_ass_searchList)
            recyclerView?.layoutManager = LinearLayoutManager(context)
            recyclerView?.adapter = mAdapter
        }
    }


    /** 数据适配器 */
    fun setAdapterData(mAdapter: PersonSearchAdapter?, list: List<JobInfoBean>?) {
        if (null != list) {
            mAdapter?.setNewData(list)
            if (list.isEmpty()) {
                showNoDataView(true)
            } else {
                setListViewShow(true)
                showNoDataView(false)
            }
        }
    }

    /** 显示暂无数据缺省页 */
    private fun showNoDataView(isNoData: Boolean) {
        val noData = getView<TextView>(R.id.btv_ass_searchNoData)
        if (isNoData) {
            noData?.visibility = View.VISIBLE
        } else {
            noData?.visibility = View.GONE
        }
    }

    /** 设置适配器的显示和历史记录布局 */
    fun setListViewShow(showList: Boolean) {
        val swipReView = getView<SwipeRefreshLayout>(R.id.srl_ass_swipeSearch)
        val history = getView<LinearLayout>(R.id.ll_ass_history)
        val noData = getView<TextView>(R.id.btv_ass_searchNoData)
        if (showList) {
            swipReView?.visibility = View.VISIBLE
            history?.visibility = View.GONE
            swipReView.bringToFront()
        } else {
            swipReView?.visibility = View.GONE
            noData?.visibility = View.GONE
            history?.visibility = View.VISIBLE
            history?.bringToFront()
        }
    }

    /**
     * 关闭键盘
     */
    fun closeInputKeyboard(context: Context) {
        LKAppUtil.getInstance().closeInput(context, getView<LKEditText>(R.id.et_ass_inputSearch))
    }
}